import { createWebHistory, createRouter } from 'vue-router'
import { updatePageTitle, checkAuth } from "@/router/authGuard.js";

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/Login.vue'),
        meta: { title: '登录' }
    },
    {
        path: '/',
        name: 'Layout',
        component: () => import('@/views/layout/Layout.vue'),
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('@/views/dashboard/Dashboard.vue'),
                meta: { title: '控制台', icon: 'dashboard' }
            },
            {
                path: 'user',
                name: 'UserList',
                component: () => import('@/views/system/user/Index.vue'),
                meta: { title: '用户管理', icon: 'user' }
            },
            {
                path: 'role',
                name: 'RoleList',
                component: () => import('@/views/system/role/Index.vue'),
                meta: { title: '角色管理', icon: 'role' }
            },
            {
                path: 'category',
                name: 'Category',
                component: () => import('@/views/system/category/Index.vue'),
                meta: { title: '分类管理', icon: 'role' }
            },
            {
                path: 'report',
                name: 'Report',
                component: () => import('@/views/finance/report/Index.vue'),
                meta: { title: '财务报表', icon: 'role' }
            },
            {
                path: 'income',
                name: 'Income',
                component: () => import('@/views/finance/income/Index.vue'),
                meta: { title: '收入明细', icon: 'UserFilled' }
            },
            {
                path: 'expense',
                name: 'Expense',
                component: () => import('@/views/finance/expense/Index.vue'),
                meta: { title: '支出明细', icon: 'UserFilled' }
            },
            {
                path: 'transfer',
                name: 'Transfer',
                component: () => import('@/views/finance/transfer/Index.vue'),
                meta: { title: '转账明细', icon: 'UserFilled' }
            },
            {
                path: 'debt',
                name: 'Debt',
                component: () => import('@/views/finance/debt/Index.vue'),
                meta: { title: '债务管理', icon: 'UserFilled' }
            },
            {
                path: 'fundAccountType',
                name: 'FundAccountType',
                component: () => import('@/views/finance/fundAccountType/Index.vue'),
                meta: { title: '账户类型', icon: 'UserFilled' }
            },
            {
                path: 'fundAccount',
                name: 'FundAccount',
                component: () => import('@/views/finance/fundAccount/Index.vue'),
                meta: { title: '资金账户', icon: 'UserFilled' }
            },
            {
                path: 'backup',
                name: 'Backup',
                component: () => import('@/views/finance/backup/Index.vue'),
                meta: { title: '数据备份与恢复', icon: 'UserFilled' }
            }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('@/views/error/NotFound.vue'),
        meta: { title: '404 Not Found', icon: 'user' }
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
})

//挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to,from,next)=>{
    // 动态设置页签标题
    updatePageTitle(to)
    // 验证登录状态
    checkAuth(to, from, next);
})

export default router